package cn.stylefeng.guns.modular.medical.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.EntityWrapper;

import cn.stylefeng.guns.core.log.LogObjectHolder;
import cn.stylefeng.guns.modular.medical.service.IMedicaltypeService;
import cn.stylefeng.guns.modular.system.model.Medicaltype;
import cn.stylefeng.roses.core.base.controller.BaseController;

/**
 * 医学期刊控制器
 *
 * @author fengshuonan
 * @Date 2020-06-21 00:35:00
 */
@Controller
@RequestMapping("/medicaltype")
public class MedicaltypeController extends BaseController {

    private String PREFIX = "/medical/medicaltype/";

    @Autowired
    private IMedicaltypeService medicaltypeService;

    /**
     * 跳转到医学期刊首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "medicaltype.html";
    }

    /**
     * 跳转到添加医学期刊
     */
    @RequestMapping("/medicaltype_add")
    public String medicaltypeAdd() {
        return PREFIX + "medicaltype_add.html";
    }

    /**
     * 跳转到修改医学期刊
     */
    @RequestMapping("/medicaltype_update/{medicaltypeId}")
    public String medicaltypeUpdate(@PathVariable Integer medicaltypeId, Model model) {
        Medicaltype medicaltype = medicaltypeService.selectById(medicaltypeId);
        model.addAttribute("item",medicaltype);
        LogObjectHolder.me().set(medicaltype);
        return PREFIX + "medicaltype_edit.html";
    }

    /**
     * 获取医学期刊列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
    	
    	EntityWrapper<Medicaltype> wrapper = new EntityWrapper<Medicaltype>();
    	wrapper.like("goods_name", condition).or()
    	.like("id", condition);
        return medicaltypeService.selectList(wrapper);
    }

    /**
     * 新增医学期刊
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Medicaltype medicaltype) {
        medicaltypeService.insert(medicaltype);
        return SUCCESS_TIP;
    }

    /**
     * 删除医学期刊
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer medicaltypeId) {
        medicaltypeService.deleteById(medicaltypeId);
        return SUCCESS_TIP;
    }

    /**
     * 修改医学期刊
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Medicaltype medicaltype) {
        medicaltypeService.updateById(medicaltype);
        return SUCCESS_TIP;
    }

    /**
     * 医学期刊详情
     */
    @RequestMapping(value = "/detail/{medicaltypeId}")
    @ResponseBody
    public Object detail(@PathVariable("medicaltypeId") Integer medicaltypeId) {
        return medicaltypeService.selectById(medicaltypeId);
    }
    
    @InitBinder
   	public void initBinder(WebDataBinder binder) {
   	    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
   	    binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));        
   	}
}
